package gui;

import database.UserOperations;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;


import model.User1;

@SuppressWarnings("serial")
public class AddNewUserSecurity extends JFrame implements ActionListener, ItemListener 
{
	private JPanel p1, p2, p3,p4;
	private JLabel l1, l2, l3, l4, l5,l6,l7,l8;
	private JButton exit, next;
	private JTextField f1, f2, f3,f4,f5;
	private JComboBox<String> c1;
	private String Qs="Mothers maiden name";
	private boolean done=false;
	private UserOperations uo;
	
	public AddNewUserSecurity() 
	{
		super("Security Screen");
		this.setSize(840, 500);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLayout(new FlowLayout());
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		uo = new UserOperations();
		
		//Panel 1
		p1 = new JPanel();
		p1.setPreferredSize(new Dimension(840 ,80));
		l1 = new JLabel("Enter username, password, and security question here.");
		p1.add(l1);		
		//p1.add(Box.createRigidArea(new Dimension(0,110)));
		
		//Panel 2
		p2 = new JPanel();
		p2.setPreferredSize(new Dimension(840 ,80));
		l2 = new JLabel("Security");
		l2.setFont(new Font("serif",Font.BOLD,26));
		p2.add(l2);	
		
		//Panel 3
		p3 = new JPanel();
		p3.setPreferredSize(new Dimension(300 ,150));
		p3.setLayout(new GridLayout(7,2));
		
		//P3 Labels and fields declaration
		l3 = new JLabel("Username:");
		l4 = new JLabel("Password:");
		l5 = new JLabel("Re-Type Password:");
		l6 = new JLabel("Security Question:");
		String[] secQs={"Mothers maiden name", "Your first pets name"};
		c1 = new JComboBox<String>(secQs);
		c1.addItemListener(this);
		l7 = new JLabel("Security Answer:");
		l8 = new JLabel("Re-Type Security Answer:");
		
		f1 = new JTextField(25);
		f2 = new JTextField(25);
		f3 = new JTextField(25);
		f4 = new JTextField(25);
		f5 = new JTextField(25);
		
		//P3 Adding fields and labels
		p3.add(l3);
		p3.add(f1);		
		p3.add(l4);
		p3.add(f2);	
		p3.add(l5);
		p3.add(f3);		
		p3.add(l6);
		p3.add(c1);
		p3.add(l7);
		p3.add(f5);		
		p3.add(l8);
		p3.add(f4);
		
		//Panel 4
		p4 = new JPanel();
		p4.setPreferredSize(new Dimension(840 ,100));
		exit = new JButton("EXIT");
		exit.addActionListener(this);
		next = new JButton("NEXT");
		next.addActionListener(this);
		p4.add(exit);
		p4.add(Box.createRigidArea(new Dimension(300,110)));
		p4.add(next);
		
		//Adding Panels to the page
		this.add(p1, BorderLayout.NORTH);
		this.add(p2, BorderLayout.NORTH);
		this.add(p3, BorderLayout.CENTER);
		this.add(p4, BorderLayout.PAGE_END);
	
	}
	
	public boolean addnewUser() 
	{
		boolean cont=false;
		String username = f1.getText();
		String password = f2.getText();
		String rePassword = f3.getText();
		String answer = f5.getText();
		String reAnswer = f4.getText();
		
		if (username.equals("") || password.equals("") || rePassword.equals("") 
				|| answer.equals("") || reAnswer.equals("")) 
		{
			JOptionPane.showMessageDialog(null,
					"Please enter details in all fields","ERROR",JOptionPane.WARNING_MESSAGE);
		} 
		else if (!password.equals(rePassword))
		{
			JOptionPane.showMessageDialog(null,
					"Passwords do not match","ERROR",JOptionPane.WARNING_MESSAGE);
		}
		else if (!answer.equals(reAnswer))
		{
			JOptionPane.showMessageDialog(null,
					"Security answers do not match","ERROR",JOptionPane.WARNING_MESSAGE);
			System.out.println(answer);
			System.out.println(reAnswer);
		}
		else 
		{
			//create a user object and pass it to UserOperations to save it
			if(checkUsername()==false)
			{
				User1 u = new User1(username, password, Qs, answer);
				uo.addUser(u);
				uo.closeDB2();
				JOptionPane.showMessageDialog(null,"User "+username+" Added");
				cont=true;
			}
			else
			{
				JOptionPane.showMessageDialog(null,
						"Username already in use, choose another username","ERROR",JOptionPane.WARNING_MESSAGE);
			}
		}
		return cont;
	}

	//check if username already in use
		public boolean checkUsername()
		{
			boolean well=false;
			ArrayList<String> list=uo.userPs();
			for(int i=0;i<list.size();i++)
			{
				if(list.get(i).equals(f1.getText()))
				{
					well=true;
				}
				else
				{
					i++;
				}
			}
			return well;
		}
	
	public static void main(String args[]) 
	{
		AddNewUserSecurity ss = new AddNewUserSecurity();
		ss.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource()==next)
		{
			done=addnewUser();
			if(done==true)
			{
				this.setVisible(false);
			}
		}
		if(ae.getSource()==exit)
		{
			this.dispose();	
		}
		
	}

	@Override
	public void itemStateChanged(ItemEvent e) 
	{
		if (e.getSource() == c1 && e.getStateChange() == ItemEvent.SELECTED)
		{
			Qs=(String)c1.getSelectedItem();
		}
		
	}

}

